const BaseController = require('./BaseController.js');

class RoleController extends BaseController {
  // 角色列表
  async role_list(){
    const { ctx } = this
    let params = await this.getParams(ctx)
    let result = await ctx.service.role.list(params)
    this.showSuccess(result)
  }

  // 创建角色
  async role_create(){
    const { ctx } = this
    let params = await this.getParams(ctx)
    let result = await ctx.service.role.create(params)
    if(result){
      this.showSuccess(result)
    }else{
      this.ApiException('该角色已存在')
    }
  }

  // 编辑角色
  async role_edit(){
    const { ctx } = this
    let params = await this.getParams(ctx)
    let result = await ctx.service.role.edit(params)
    if(result){
      this.showSuccess(result)
    }else{
      this.ApiException('不能修改为已存在的角色名称')
    }
  }

  // 删除角色
  async role_delete(){
    const { ctx } = this
    let params = await this.getParams(ctx)
    let result = await ctx.service.role.deleteItem(params)
    if(result){
      this.showSuccess(result)
    }else{
      this.ApiException('关联了账号，不可删除！')
    }
  }
}

module.exports = RoleController;